package exam;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.exam.domain.Instructor;


public class CatalogServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CatalogServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		CatalogBean Catalog = new CatalogBean();
		Instructor ins=(Instructor)request.getSession().getAttribute("insinfo");
		String submit1=request.getParameter("submit");
		
		if(submit1.equals("true"))
		{
			String type=request.getParameter("type");
			if(type.equals("add"))
			{
				Catalog.setCatalog_name(request.getParameter("catalog_name"));
				Catalog.setCourse_id(request.getParameter("course_id"));
				Catalog.setContent(request.getParameter("content"));
				Catalog.addCatalog();
				response.sendRedirect(request.getContextPath()+"/tea_courses.jsp");
			}
			else if(type.equals("modify"))
			{
				Catalog.setCatalog_id(request.getParameter("catalog_id"));
				String cata_id=Catalog.getCatalog_id();
				CatalogBean cat=Catalog.Catalog_modify(cata_id);
				request.getSession().setAttribute("Catalog", cat);
				response.sendRedirect(request.getContextPath()+"/tea_catalogModify.jsp");
			}
			else if(type.equals("change"))
			{
				Catalog.setCatalog_id(request.getParameter("catalog_id"));
				Catalog.setCatalog_name(request.getParameter("catalog_name"));
				Catalog.setContent(request.getParameter("content"));
				Catalog.setCourse_id(request.getParameter("course_id"));
				Catalog.update_catalog();
				response.sendRedirect(request.getContextPath()+"/tea_pointManage.jsp");
			}
			else if(type.equals("delete"))
			{
				Catalog.setCatalog_id(request.getParameter("catalog_id"));
				Catalog.delete_Catalog();
				System.out.print(Catalog.getCatalog_id());
				response.sendRedirect(request.getContextPath()+"/tea_pointManage.jsp");
			}
		}
		else if(submit1.equals("false"))
		{
			String type= request.getParameter("type");
			if(type.equals("link_to_add"))
			{
				List<Courseinfo> courselist=Catalog.findcourse(ins.getI_id());
				request.getSession().setAttribute("courselist", courselist);
				response.sendRedirect(request.getContextPath()+"/tea_addpoint.jsp");
			}
			else if(type.equals("link_to_modify"))
			{
				List<Courseinfo> courselist=Catalog.findcourse(ins.getI_id());
				request.getSession().setAttribute("courselist", courselist);
				response.sendRedirect(request.getContextPath()+"/tea_pointManage.jsp");
			}
			else if(type.equals("modify"))
			{
				
				String course_id=request.getParameter("course_id");
				List<CatalogBean> Catalist= Catalog.refreshCatalog(course_id) ;
				String isrefresh="true";
				request.getSession().setAttribute("Catalist", Catalist);
				request.getSession().setAttribute("isrefresh", isrefresh);
				response.sendRedirect(request.getContextPath()+"/tea_pointManage.jsp");
			}
		}
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
